package com.zyf.workflow.test.work.firmware;

import com.zyf.workflow.node.Node;
import com.zyf.workflow.node.WorkNode;
import com.zyf.workflow.test.work.firmware.bean.FirmwareBean;
import com.zyf.workflow.test.work.firmware.worker.FirmwareUpgradeWorker;
import com.zyf.workflow.work.AbstractWork;
import com.zyf.workflow.work.bean.WorkTypeEnum;
import com.zyf.workflow.worker.Worker;

import java.util.ArrayList;
import java.util.List;

public class FirmwareUpgradeInbandWork<T> extends AbstractWork<FirmwareBean> {

    public FirmwareUpgradeInbandWork(FirmwareBean firmwareBean, String workName) {
        super(firmwareBean, workName);
    }

    @Override
    public List<Node<?>> initNodes(FirmwareBean data) {
        List<Node<?>> nodes = new ArrayList<>();
        Node<String> configVisualMediaNode = getConfigVisualMediaNode(data);
        Node<String> setCdromBootNode = getSetCdromBootNode(data);
        Node<String> process = getProcess(data);
        nodes.add(configVisualMediaNode);
        nodes.add(setCdromBootNode);
        nodes.add(process);
        return nodes;
    }

    @Override
    public WorkTypeEnum getWorkType() {
        return WorkTypeEnum.WORK;
    }


    /**
     * 配置虚拟介质节点
     *
     * @param firmwareBean
     * @return
     */
    private Node<String> getConfigVisualMediaNode(FirmwareBean firmwareBean) {

        FirmwareUpgradeWorker firmwareUpgradeWorker = new FirmwareUpgradeWorker();
        Worker<FirmwareBean, String, String> worker = firmwareUpgradeWorker::configVisualMedia;

        return new WorkNode.Builder<FirmwareBean, String>()
                .withWorker(worker)
                .withNodeId(1)
                .withData(firmwareBean)
                .withName(getWorkName() + "1")
                .setCallBack((data, param) -> {
                    System.out.println("config visual media Finish, callback called");
                    System.out.println(".....");
                })
                .create();

    }


    private Node<String> getSetCdromBootNode(FirmwareBean firmwareBean) {

        FirmwareUpgradeWorker firmwareUpgradeWorker = new FirmwareUpgradeWorker();
        Worker<FirmwareBean, String, String> worker = firmwareUpgradeWorker::configCDBoot;

        return new WorkNode.Builder<FirmwareBean, String>()
                .withWorker(worker)
                .withNodeId(2)
                .withData(firmwareBean)
                .withName(getWorkName() + "2")
                .setCallBack((data, param) -> {
                    System.out.println("server " + data.getResourceBean().getIp() + " set cdroom boot and restart finished");
                    System.out.println(".....");
                })
                .create();

    }


    private Node<String> getProcess(FirmwareBean firmwareBean) {

        FirmwareUpgradeWorker firmwareUpgradeWorker = new FirmwareUpgradeWorker();
        Worker<FirmwareBean, String, Void> worker = firmwareUpgradeWorker::getProcess;

        return new WorkNode.Builder<FirmwareBean, String>()
                .withWorker(worker)
                .withNodeId(3)
                .withData(firmwareBean)
                .withName(getWorkName() + "3")
                .setCallBack((data, param) -> {
                    System.out.println("server " + data.getResourceBean().getIp() + " get process finished");
                    System.out.println(".....");
                })
                .create();

    }


}
